package admin

import (
	"github.com/gin-gonic/gin"
	"mojieshi/define"
	"mojieshi/models"
	"net/http"
	"strconv"
	"time"
)

// 获取分组
func ShopGroupList(c *gin.Context) {
	var jsonData Newdata
	err := c.ShouldBindJSON(&jsonData)
	if err != nil {
		define.Response.ResultFail(c, 10000, define.JsonData, "")
		return
	}
	var list []models.MShopGroup
	var count int64
	if jsonData.Page == 0 {
		jsonData.Page = -1
	}
	if jsonData.Limit == 0 {
		jsonData.Limit = -1
	}
	offsetVal := (jsonData.Page - 1) * jsonData.Limit
	if jsonData.Page == -1 && jsonData.Limit == -1 {
		offsetVal = -1
	}
	tx := models.DB.Model(new(models.MShopGroup)).Where("aid=? AND name LIKE ?", jsonData.Aid, "%"+jsonData.Name+"%")
	if jsonData.Id > 0 {
		tx = tx.Where("status=?", jsonData.Id)
	}
	if jsonData.Type > 0 {
		tx = tx.Where("Type=?", jsonData.Type)
	}
	if jsonData.Jid > 0 {
		tx = tx.Where("from_id=?", jsonData.Jid)
	}
	if jsonData.SubName != "" {
		tx = tx.Where("sub_name LIKE ?", "%"+jsonData.SubName+"%")
	}
	tx = tx.Count(&count).Limit(jsonData.Limit).Offset(offsetVal).Order("sort desc,id").Find(&list)
	if err != nil {
		define.Response.ResultFail(c, 10001, err.Error(), "")
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"code":    0,
		"data": map[string]interface{}{
			"list":  list,
			"count": count,
		},
	})
}

// 新增/修改商品分组
func ShopGroupAdd(c *gin.Context) {
	//获取传值
	var jsonData models.MShopGroup
	err := c.ShouldBindJSON(&jsonData)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  err,
		})
		return
	}
	if jsonData.ID > 0 {
		//修改数据
		var editData = map[string]interface{}{
			"name":     jsonData.Name,
			"status":   jsonData.Status,
			"pic":      jsonData.Pic,
			"sub_name": jsonData.SubName,
			"is_row":   jsonData.IsRow,
			"from_id":  jsonData.FromId,
			"type":     jsonData.Type,
			"banner":   jsonData.Banner,
		}
		err = models.DB.Model(new(models.MShopGroup)).Where("id=?", jsonData.ID).Updates(&editData).Error
		if err != nil {
			c.JSON(http.StatusOK, gin.H{
				"code": 10003,
				"msg":  err.Error(),
			})
			return
		}
		//写入日志
		userId, _ := strconv.Atoi(c.GetHeader("userId")) //将字符串转为int
		userName, bid := models.UserName(userId)
		logData := "管理员：" + userName + "，修改了商品分组:" + strconv.Itoa(jsonData.ID) + "的内容"
		models.LogAdd(3, "修改商品分组", userId, logData, bid, jsonData.Aid)
	} else {
		var data models.MShopGroup
		data.Name = jsonData.Name
		data.Pic = jsonData.Pic
		data.Status = jsonData.Status
		data.Aid = jsonData.Aid
		data.SubName = jsonData.SubName
		data.IsRow = jsonData.IsRow
		data.FromId = jsonData.FromId
		data.Type = jsonData.Type
		data.Banner = jsonData.Banner
		data.Createtime = int(time.Now().Unix())
		err = models.DB.Model(new(models.MShopGroup)).Create(&data).Error
		if err != nil {
			c.JSON(http.StatusOK, gin.H{
				"code": 10001,
				"msg":  err.Error(),
			})
			return
		}
		//写入日志
		userId, _ := strconv.Atoi(c.GetHeader("userId")) //将字符串转为int
		userName, bid := models.UserName(userId)
		logData := "管理员：" + userName + "，新增了一个商品分组:" + jsonData.Name
		models.LogAdd(2, "新增商品分组", userId, logData, bid, jsonData.Aid)
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"code":    0,
		"msg":     "成功！",
	})
}

// 删除商品分组
func ShopGroupDel(c *gin.Context) {
	var jsonData models.MShopGroup
	err := c.ShouldBindJSON(&jsonData)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  err,
		})
		return
	}
	var data models.MShopGroup
	models.DB.Model(new(models.MShopGroup)).Where("id=?", jsonData.ID).First(&data)
	tx := models.DB.Begin()

	//删除
	err = tx.Delete(&models.MShopGroup{}, jsonData.ID).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10004,
			"msg":  err.Error(),
		})
		tx.Rollback()
		return
	}
	tx.Commit()

	//写入日志
	userId, _ := strconv.Atoi(c.GetHeader("userId")) //将字符串转为int
	userName, bid := models.UserName(userId)
	logData := "管理员：" + userName + "，删除了一个商品分组:" + data.Name
	models.LogAdd(4, "删除商品分组", userId, logData, bid, jsonData.Aid)
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"code":    0,
		"msg":     "成功！",
	})
}

// 商品分组排序
func ShopGroupSort(c *gin.Context) {
	//接收id
	var jsonData models.MShopGroup
	err := c.ShouldBindJSON(&jsonData)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  err,
		})
		return
	}
	var data models.MShopGroup
	models.DB.Model(new(models.MShopGroup)).Where("id=?", jsonData.ID).First(&data)
	//修改
	err = models.DB.Model(new(models.MShopGroup)).Where("id=?", jsonData.ID).Update("sort", jsonData.Sort).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10002,
			"msg":  err.Error(),
		})
		return
	}
	//写入日志
	userId, _ := strconv.Atoi(c.GetHeader("userId")) //将字符串转为int
	userName, bid := models.UserName(userId)
	logData := "管理员：" + userName + "，修改分组:" + data.Name + "的排序"
	models.LogAdd(5, "分组排序", userId, logData, bid, jsonData.Aid)
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"code":    0,
		"msg":     "成功！",
	})
}
